<template>
  <div class="container">
    <div class="header">
      <el-input
        v-model="searchQuery"
        placeholder="请输入员工姓名"
        prefix-icon="el-icon-search"
        clearable
        @keyup.enter.native="debouncedSearch"  
        @click.native="debouncedSearch" 
      ></el-input>
      <el-button icon="el-icon-plus" @click="handleAddEmployee"
        >添加员工</el-button
      >
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="name" label="员工姓名"> </el-table-column>
      <el-table-column prop="username" label="账号"></el-table-column>
      <el-table-column prop="phone" label="手机号"> </el-table-column>
      <el-table-column prop="status" label="账号状态">
        <template v-slot="{ row }">
          {{ row.status ? "正常" : "已禁用" }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-link type="primary" @click="bianji(row.id)">编辑</el-link>
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm
            title="确认调整该账号的状态?"
            @onConfirm="handleChangeStauts(row)"
          >
            <el-link type="danger" slot="reference">{{
              row.status ? "禁用" : "启用"
            }}</el-link>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="footer">
      <Pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageInfo.page"
          :page-size="pageInfo.pageSize"
          :total="total"
        />
    </div>
  </div>
</template>

<script>
import { getEmployees, changeStauts } from "@/api/employee";
import { debounce } from "lodash";
import Pagination from '@/components/Pagination/index.vue'
export default {
  components:{Pagination},
  data() {
    return {
      input: "",
      searchQuery: "",
      tableData: [],
      pageInfo: {
        page: 1,
        pageSize: 10,
      },
      total: 0,
      searchTimer: null,
      debouncedSearch: null
    };
  },
  watch: {
  searchQuery(newVal) {
    if (!newVal) { // 只有当搜索词为空时自动刷新
      this.handleSearch();
    }else {
        // 使用防抖函数替代直接调用
        this.debouncedSearch();
    }
  }
},
  created() {
    this.debouncedSearch = debounce(this.handleSearch, 500);
    this.init();
  },
  beforeDestroy() {
    // 5. 组件销毁前取消防抖（避免内存泄漏）
    if (this.debouncedSearch) {
      this.debouncedSearch.cancel();
    }
  },
  methods: {
    async init() {
      try {
        const params = {
          ...this.pageInfo,
          name: this.searchQuery.trim(), // 添加搜索参数
        };

        const res = await getEmployees(params);
        this.tableData = res.data.records;
        this.total = res.data.total;
      } catch (error) {
        console.error("获取员工列表失败:", error);
      }
    },
     // 新增：点击搜索图标时触发
  handleSearch() {
    this.pageInfo.page = 1; // 重置到第一页
    this.init();
  },
    handleAddEmployee() {
      this.$router.push({ name: "AddEmployee", query: { isEdit: "0" } });
    },
    handleSizeChange(val) {
      this.pageInfo.page = 1;
      this.pageInfo.pageSize = val;
      this.init();
    },
    handleCurrentChange(val) {
      this.pageInfo.page = val;
      this.init();
    },
    async handleChangeStauts(row) {
      await changeStauts({
        id: row.id,
        status: +!row.status,
      });
      this.$message.success("修改状态成功");
      this.init();
    },
    bianji(id) {
      this.$router.push({ name: "EditEmployee", query: { isEdit: "1", id } });
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  padding: 33px;
  .header {
    display: flex;
    justify-content: space-between;
    .el-input {
      width: 250px;
    }
  }
  .el-table {
    margin-top: 20px;
  }
  .footer {
    margin-top: 20px;
    display: flex;
    justify-content: center;
  }
}
</style>

